package com.thinkgem.jeesite.frame.pm.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.frame.pm.entity.PmDataTree;
import com.thinkgem.jeesite.frame.pm.entity.PmDataTreeItem;
import com.thinkgem.jeesite.frame.pm.service.PmDataTreeItemService;
import com.thinkgem.jeesite.frame.pm.service.PmDataTreeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 树形字典条目Controller
 *
 * @author kongxiangsheng
 * @version 2018-09-02
 */
@Controller
@RequestMapping(value = "${adminPath}/pm/pmDataTreeItem")
public class PmDataTreeItemController extends BaseController {
    @Autowired
    private PmDataTreeItemService pmDataTreeItemService;
    @Autowired
    private PmDataTreeService pmDataTreeService;

    @RequiresPermissions("pm:pmDataTreeItem:view")
    @RequestMapping(value = {"listByDataTreeId", ""})
    public String listByDataTreeId(String dataTreeId, Model model) {
        PmDataTree pmDataTree = pmDataTreeService.getById(dataTreeId);
        List<PmDataTreeItem> list = pmDataTreeItemService.getByPmDataTreeId(dataTreeId);

        model.addAttribute("pmDataTree", pmDataTree);
        model.addAttribute("list", list);
        return "frame/pm/PmDataTreeItem/list";
    }

    @RequiresPermissions("pm:pmDataTreeItem:view")
    @RequestMapping(value = "form")
    public String form(PmDataTreeItem pmDataTreeItem, Model model) {
        // 查询数据字典信息
        PmDataTree pmDataTree = pmDataTreeService.getById(pmDataTreeItem.getDataTreeId());

        model.addAttribute("pmDataTree", pmDataTree);
        model.addAttribute("pmDataTreeItem", pmDataTreeItem);
        return "frame/pm/PmDataTreeItem/form";
    }

    @RequiresPermissions("pm:pmDataTreeItem:edit")
    @RequestMapping(value = "save")
    public String save(PmDataTreeItem pmDataTreeItem, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, pmDataTreeItem)) {
            return form(pmDataTreeItem, model);
        }

        pmDataTreeItemService.saveDataItem(pmDataTreeItem);
        addMessage(redirectAttributes, "保存树形字典条目成功");
        return "redirect:" + adminPath + "/pm/pmDataTreeItem/listByDataTreeId?repage&dataTreeId=" + pmDataTreeItem.getDataTreeId();
    }

    @RequiresPermissions("pm:pmDataTreeItem:edit")
    @RequestMapping(value = "delete")
    public String delete(PmDataTreeItem pmDataTreeItem, RedirectAttributes redirectAttributes) {
        pmDataTreeItemService.deleteDataItem(pmDataTreeItem);
        addMessage(redirectAttributes, "删除树形字典条目成功");
        return "redirect:" + adminPath + "/pm/pmDataTreeItem/listByDataTreeId?repage&dataTreeId=" + pmDataTreeItem.getDataTreeId();
    }

    /**
     * 根据Id获取名称
     *
     * @param id
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeValue")
    public String treeValue(@RequestParam(required = true) String id, HttpServletResponse response) {
        // 是否是根节点
        if (BaseConstants.ROOT_TREE_ITEM_ID.equals(id)) {
            return "根节点";
        }

        // 查找记录
        PmDataTreeItem pmDataTreeItem = pmDataTreeItemService.getById(id);
        if (null != pmDataTreeItem) {
            return pmDataTreeItem.getName();
        }
        return "";
    }

    /**
     * 获取树形结构
     *
     * @param extId
     * @param dataTreeId
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, String dataTreeId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();

        // 添加根节点
        Map<String, Object> rootMap = Maps.newHashMap();
        rootMap.put("id", BaseConstants.ROOT_TREE_ITEM_ID);
        rootMap.put("pId", BaseConstants.ROOT_TREE_ITEM_ID);
        rootMap.put("name", "根节点");
        mapList.add(rootMap);

        // 添加子节点
        List<PmDataTreeItem> list = pmDataTreeItemService.getByPmDataTreeId(dataTreeId);
        for (int i = 0; i < list.size(); i++) {
            PmDataTreeItem pmDataTreeItem = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(pmDataTreeItem.getId()) && pmDataTreeItem.getParentIds().indexOf("," + extId + ",") == -1)) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", pmDataTreeItem.getId());
                map.put("pId", pmDataTreeItem.getParentId());
                map.put("name", pmDataTreeItem.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }

}